package com.yuan.controller.admin;

import com.yuan.exception.Assert;
import com.yuan.pojo.entity.Admin;
import com.yuan.pojo.vo.LoginVO;
import com.yuan.pojo.vo.UserInfoVO;
import com.yuan.result.R;
import com.yuan.result.ResponseEnum;
import com.yuan.service.AdminService;
import com.yuan.util.JwtUtils;
import com.yuan.util.RegexValidateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Api(tags = "管理员登录")
@RestController
@RequestMapping("/admin/core")
@Slf4j
public class AdminLoginController {

    @Resource
    private AdminService adminService;

    @ApiOperation("管理员登录")
    @PostMapping("/login")
    public R login(@RequestBody Admin admin, HttpServletRequest request){
        String username = admin.getUsername();
        String password = admin.getPassword();

        //MOBILE_NULL_ERROR(-202, "手机号不能为空"),
        Assert.notEmpty(username, ResponseEnum.USERNAME_NULL_ERROR);
        //PASSWORD_NULL_ERROR(-204, "密码不能为空"),
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);

        String ip = request.getRemoteAddr();

        Admin adminInfo =  adminService.login(admin,ip);

        return R.success().data("adminInfo",adminInfo).message("登录成功");
    }

    @ApiOperation("校验令牌")
    @GetMapping("/checkToken/")
    public R checkToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        boolean checkToken = JwtUtils.checkToken(token);

        if (checkToken){
            return R.success();
        }else {
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
    }
}
